<?php

namespace App\Controller;

class TaskController extends AppBaseController
{
    private $adLogic;
    private $taskLogic;
    private $joinLogic;
    private $userLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->adLogic = A('App/Ad', 'Logic');
        $this->taskLogic = A('App/Task', 'Logic');
        $this->joinLogic = A('App/Join', 'Logic');
        $this->userLogic = A('App/User', 'Logic');
    }

    /**
     * 任务列表.
     */
    public function home()
    {
        $type = I('get.type');
        $type = $type ? $type : 2;

        $adList = $this->adLogic->getTaskIndexBannerAd();
        $taskCount = $this->taskLogic->statistics(array(
            'type' => $type,
        ));
        $joinCount = $this->joinLogic->statistics(array(
            // "task_type" => $type
        ));
        $finishCount = $this->joinLogic->statistics(array(
            // "task_type" => $type,
            'state' => array(array('eq', 2), array('eq', 3), 'or'),
        ));

        //获取用户数量
        $userCount = $this->userLogic->statistics();
    
        $this->assign('type', $type);
        $this->assign('adList', $adList);
        $this->assign('taskCount', $taskCount);
        $this->assign('joinCount', $joinCount + 8532 + $userCount);
        $this->assign('finishCount', $finishCount + 1652);
        $this->display();
    }

    /**
     * 获取任务列表.
     */
    public function doGetTaskList()
    {
        $taskType = I('post.taskType');
        $page = I('post.page');
        $page = $page ? $page : 1;
        $size = 20;

        $now = date('Y-m-d H:i:s');
        $where = array(
            'begin_time' => array('elt', $now),
            'end_time' => array('egt', $now),
            'type' => $taskType,
        );

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $listRes = $this->taskLogic->getList($page, $size, $where, $userId);
        if (!static::$res->isSuc($listRes)) {
            $this->ajaxReturn($listRes);
        }

        $data = $listRes['Result'];
        $list = $data['list'];
        $count = $data['count'];

        if (empty($list)) {
            $this->ajaxReturn(static::$res->err('暂无数据'));
        }

        $countPage = ceil($count / $size);
        $hasNext = $countPage > $page ? true : false;

        $this->assign('list', $list);
        $html = $this->fetch('doGetTaskList');

        $res = static::$res->suc('成功', array(
            'html' => $html,
            'list' => $list,
            'count' => $count,
            'hasNext' => $hasNext,
        ));
        $this->ajaxReturn($res);
    }

    /**
     * 查看任务明细.
     */
    public function detial()
    {
        $id = I('get.id');
        $infoRes = $this->taskLogic->getDetial($id);
        if (!static::$res->isSuc($infoRes)) {
            $this->mobileErr($infoRes['Message']);
        }
        $info = $infoRes['Result'];
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $info['notice'] = htmlspecialchars_decode($info['notice']);
        $info['info'] = htmlspecialchars_decode($info['info']);

        $joinWhere = array(
            'user_id' => $userId,
            'task_id' => $id,
            'state' => array('neq', -1),
        );
        $joinInfoRes = $this->joinLogic->findJoinInfo($joinWhere);
        $joinInfo = $joinInfoRes['Result'];

        $joinUserListRes = $this->joinLogic->getJoinUserList(1, 8, array(
            'task_id' => $id,
            'state' => array('neq', -1),
        ));
        $joinUserList = $joinUserListRes['Result']['list'];

        $this->assign('info', $info);
        $this->assign('joinInfo', $joinInfo);
        $this->assign('joinUserList', $joinUserList);
        $this->display();
    }

    /**
     * 排行榜.
     */
    public function ranking()
    {
        $type = I('get.type');
        if (empty($type)) {
            $type = 'day';
        }
        $rankingRes = $this->taskLogic->ranking($type);
        if (static::$res->isErr($rankingRes)) {
            $this->sysErr($rankingRes['Message']);
        }
        $list = $rankingRes['Result'];

        $first = $list[0];
        $second = $list[1];
        $third = $list[2];
        $list = array_slice($list, 3);

        $this->assign('first', $first);
        $this->assign('second', $second);
        $this->assign('third', $third);
        $this->assign('list', $list);
        $this->assign('type', $type);
        $this->display();
    }
}
